package com.shengqiang.cloud.ups.service;

import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.google.common.collect.Maps;
import com.google.gson.Gson;
import com.shengqiang.cloud.ups.common.model.JsonResult;
import com.shengqiang.cloud.ups.common.model.PageResult;
import com.shengqiang.cloud.ups.dao.modal.UpmsPermission;
import com.shengqiang.cloud.ups.dao.modal.UpmsRole;
import com.shengqiang.cloud.ups.dao.modal.UpmsUserPermission;
import com.shengqiang.cloud.ups.dao.repository.master.UpsUserMapper;
import com.shengqiang.cloud.ups.dao.service.UpsUserService;
import com.shengqiang.cloud.ups.dao.modal.UpmsUser;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import springfox.documentation.spring.web.json.Json;

import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2017/11/15.
 */
@Service
public class UpsUserWebService extends ServiceImpl<UpsUserMapper,UpmsUser> {

    @Autowired
    private UpsUserMapper userDao;

    private final static Logger logger= LoggerFactory.getLogger(UpsUserWebService.class);

    /**
     * 添加用户信息
     * @param user
     * @return
     */
    public boolean addUser(UpmsUser user){
        boolean flag=false;
        int result=userDao.insert(user);
        if(result>0){
            flag=true;
        }

        return flag;
    }

    /**
     * 分页查询玩家信息
     * @return
     */
    public PageResult<UpmsUser> selectUserPage(int current, int limit,int locked,String keyWord){
        PageResult<UpmsUser> userPageResult=null;
        Page<UpmsUser> page=new Page<>(current,limit);
        Map<String,Object> condition= Maps.newHashMap();
        condition.put("locked",locked);
        condition.put("keyWord",keyWord);
        page.setRecords(baseMapper.selectUpmsUsers(page,condition));
        int count=page.getTotal();
        List<UpmsUser> users=page.getRecords();

//        Map<String,Object> params=Maps.newHashMap();
//        params.put("users",users);
//
//        List<Map<String,Object>> userRoles=baseMapper.selectUserRoles(params);
//
//        if(userRoles!=null){
//            for (Map<String,Object> rolesMap:userRoles){
//                for (UpmsUserPermission userPermission:users){
//                    if(Integer.valueOf(rolesMap.get("userId").toString())==userPermission.getId()){
//                        userPermission.setRoles((List<UpmsRole>) rolesMap.get("roles"));
//                        break;
//                    }
//                }
//            }
//        }

        if(users!=null){
            userPageResult=new PageResult<>(0,"success",count,users);
        }else{
            userPageResult=new PageResult<>(1,"fail",0,null);
        }
        return userPageResult;
    }

    /**
     * 批量审核用户
     * @param userIds
     * @param state 0.禁用 1.审核通过
     * @return
     */
    public JsonResult batchAuditUser(String userIds,int state){
        JsonResult result=null;
        String[] ids=userIds.split(",");
        Map<String,Object> params=Maps.newHashMap();
        params.put("userIds",ids);
        params.put("state",state);
        int count=baseMapper.batchAuditUsers(params);
        if(count>0){
            return new JsonResult(1,"更新成功,更新【"+count+"】条数据");
        }else{
            return new JsonResult(0,"更新失败");
        }
    }

    public JsonResult updateUser(Map<String,Object> params){
        JsonResult result=new JsonResult(1,"修改【"+params.get("realname").toString()+"】用户信息出现异常");
        int count=baseMapper.updateUser(params);
        if(count>0){
            result.setCode(0);
            result.setMsg("修改【"+params.get("realname").toString()+"】用户信息成功");
            return result;
        }else{
            return result;
        }
    }

    @Transactional
    public JsonResult saveUserRole(Map<String,Object> params){
        JsonResult result=null;
        try{
             //先删除角色关联信息在添加
             int deleteRows=baseMapper.deleteRolesByUserId(Integer.valueOf(params.get("userId").toString()));
             int rows=baseMapper.saveUserRole(params);
            return new JsonResult(0,"分配用户角色成功");
        }catch (Exception ex){
            logger.error("分配用户角色出现异常:{}",ex.getMessage(),ex);
            return new JsonResult(1,"分配用户角色出现失败");
        }
    }

    public JsonResult<List<UpmsRole>> selectUserRoles(int userId){
        JsonResult<List<UpmsRole>> result=null;
        try{
            List<UpmsRole> upmsRoles=baseMapper.selectUserRolesByUserId(userId);
            return new JsonResult<>(0,"success",upmsRoles);
        }catch (Exception ex){
            logger.error("查询玩家角色信息出现异常:{}",ex.getMessage());
            return new JsonResult<>(1,"获取玩家角色列表出现错误");
        }
    }

    @Transactional
    public JsonResult savePrivatePermissions(Map<String,Object> params){
        try{
            int delRows=baseMapper.deleteUserPrivatePermission(params);
            int addRows=baseMapper.saveUserPrivatePermissions(params);
            return new JsonResult(0,"分配权限成功");
        }catch (Exception ex){
            logger.error("分配用户使用权限出现异常:{}",ex.getMessage(),ex);
            return new JsonResult(1,"分配使用权限出现错误");
        }
    }

    public JsonResult<List<UpmsPermission>> selectUserPermissions(int userId){
        JsonResult<List<UpmsPermission>> result=null;
        try{
            List<UpmsPermission> permissions=baseMapper.selectUserPermissions(userId);
            return new JsonResult<>(0,"success",permissions);
        }catch (Exception ex){
            logger.error("查询玩家私有权限信息出现异常:{}",ex.getMessage(),ex);
            return new JsonResult<>(1,"查询玩家私有信息出现错误");
        }
    }
}
